package com.example.mall.service;

import com.example.mall.dao.*;
import com.example.mall.dto.response.CommentResponse;
import com.example.mall.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommoityInfoService {

    @Autowired
    private CommodityInfoDao commodityInfoDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private PersonInfoDao personInfoDao;
    @Autowired
    private TagDao tagDao;
    @Autowired
    private BelongDao belongDao;
    @Autowired CommentDao commentDao;

    //查看商品信息
    public CommodityInfo getCommodityInfo(Integer cID) {
        CommodityInfo commodityInfo = commodityInfoDao.getById(cID);
        return commodityInfo;
    }

    public boolean updatePrice(Integer cID, Integer newPrice) {
        CommodityInfo commodityInfo = commodityInfoDao.getById(cID);
        if (commodityInfo == null)
            return false;

        commodityInfo.setPrice(newPrice);
        int result = commodityInfoDao.updatePriceById(commodityInfo);
        return result == 1;
    }

    //查看商品评论
    public List<CommentResponse> getCommodityComment(Integer cID) {
        List<Comment> commentList = commentDao.getListByCid(cID);
        List<CommentResponse> commentResponseList = new ArrayList<>();
        for(Comment comment:commentList) {
            CommentResponse commentResponse = new CommentResponse();
            Account account = accountDao.getById(comment.getaID());
            commentResponse.setUserName(account.getUsername());
            commentResponse.setAvatl(personInfoDao.getById(account.getpID()).getAvatl());
            commentResponse.setContent(comment.getContent());
            commentResponseList.add(commentResponse);
        }
        return commentResponseList;
    }

    public boolean updateDescription(Integer cID, String newDescription) {
        CommodityInfo commodityInfo = commodityInfoDao.getById(cID);
        if (commodityInfo == null)
            return false;

        commodityInfo.setDescription(newDescription);
        int result = commodityInfoDao.updateDescriptionById(commodityInfo);
        return result == 1;
    }

    public boolean updateDelivery(Integer cID, String newDelivery) {
        CommodityInfo commodityInfo = commodityInfoDao.getById(cID);
        if (commodityInfo == null)
            return false;

        commodityInfo.setDelivery(newDelivery);
        int result = commodityInfoDao.updateDeliveryById(commodityInfo);
        return result == 1;
    }

    //添加商品信息
    public boolean addCommodity(Integer sellerID, List<String> tagnameList, String commodityName, String description, String image, Integer price, String delivery, Integer remain) {
        if (accountDao.getById(sellerID) == null)
            return false;

        CommodityInfo commodityInfo = new CommodityInfo();
        commodityInfo.setName(commodityName);
        commodityInfo.setPrice(price);
        commodityInfo.setImage(image);
        commodityInfo.setDescription(description);
        commodityInfo.setDelivery(delivery);
        commodityInfo.setRemain(remain);
        commodityInfo.setSellerID(sellerID);
        commodityInfoDao.insert(commodityInfo);

        Integer cID = commodityInfoDao.getBySIdName(sellerID, commodityName).getId();
        for (String tagname: tagnameList) {
            Integer tID = tagDao.getByName(tagname).getId();
            Belong belong = new Belong();
            belong.settID(tID);
            belong.setcID(cID);
            belongDao.insert(belong);
        }

        return true;
    }

    public void addTag(String tagName) {
        Tag tag = new Tag();
        tag.setName(tagName);
        tagDao.insert(tag);
    }

    //查看个人库存商品列表
    public List<CommodityInfo> getCommodityList(Integer sellerID) {
        return commodityInfoDao.getBySId(sellerID);
    }

    //下/上架商品(根据商品id)
    public boolean updateCommodityStatus(Integer cID,String flag) {
        CommodityInfo commodityInfo = commodityInfoDao.getById(cID);
        if (commodityInfo == null)
            return false;

        commodityInfo.setShelves(Short.parseShort(flag));
        return commodityInfoDao.updateShelves(commodityInfo) == 1;
    }

    public void deleteCommodity(Integer cID){
        commodityInfoDao.delete(cID);
    }

    //筛选商品
    public List<CommodityInfo> filterCommodity(Integer sellerID, Integer cID, Short shelves, String tagName, String name) {
        String scID = "%", sshelves = "%";
        if (cID != null) scID = String.valueOf(cID);
        if (shelves != null) sshelves = String.valueOf(shelves);
        if (tagName == null) tagName = "%";
        if (name == null) name = "%";
        return commodityInfoDao.get(sellerID, scID, sshelves, tagName, name);
    }

}
